package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.dao.WebLoggerDao;
import com.ygqh.baby.model.WebLogger;
import com.ygqh.baby.service.WebLoggerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: sunshuo
 * @date: 2018/11/27 15:41
 */
@Service
public class WebLoggerServiceImpl implements WebLoggerService {

	@Autowired
	private WebLoggerDao webLoggerDao;

	@Override
	public void save(WebLogger loggerEntity) {
		webLoggerDao.saveEntity(loggerEntity);
	}

	@Override
	public ResultSet<WebLogger> search(QueryInfo queryInfo, String userName, Date startTime, Date endTime, String title) {
		Map<String, Object> conditions = new HashMap<>();
		if (StringUtils.isNotBlank(userName)) {
			conditions.put("userName", userName);
		}
		if (startTime != null) {
			conditions.put("startTime", startTime.getTime());
		}
		if (endTime != null) {
			conditions.put("endTime", endTime.getTime());
		}
		if (StringUtils.isNotBlank(title)) {
			conditions.put("title", title);
		}
		long count = webLoggerDao.getEntityCount(conditions);
		List<WebLogger> loggerList = null;
		if (count > 0) {
			Map<String, String> orders = new HashMap<>();
			orders.put("requestTime", "desc");
			loggerList = webLoggerDao.getPageEntityList(queryInfo, conditions, orders);
		}
		return new ResultSet<>(count, loggerList);
	}
}
